﻿using IncidentesWIN.ProxyLocal;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace IncidentesWIN
{
    public partial class FrmReporte : Form
    {
        public FrmReporte()
        {
            InitializeComponent();
        }

        private void btnConsultar_Click(object sender, EventArgs e)
        {
             string fecha1 = dtpFecha1.Value.ToString("dd/MM/yyyy");
            string fecha2 = dtpFecha2.Value.ToString("dd/MM/yyyy");
            
            DataView dv;
            //ProxiIncidentes.TB_IncidentesWSSoapClient _TB_IncidentesSOAP = new ProxiIncidentes.TB_IncidentesWSSoapClient();
            ProxyLocal.TB_IncidentesWS _TB_IncidentesSOAP = new ProxyLocal.TB_IncidentesWS();
            ProxyLocal.Gne_EstadisticaBE _Gne_EstadisticaBE = new ProxyLocal.Gne_EstadisticaBE();
            ProxiIncidentes.TB_IncidentesWSSoapClient _TB_IncidentesSOAPX = new ProxiIncidentes.TB_IncidentesWSSoapClient();

            List<ProxyLocal.Gne_EstadisticaBE> ltGne_EstadisticaBE;

            //var jsons = _TB_IncidentesSOAPX.EstadisticaTB_IncidentesRegistroByDepartamento(fecha1, fecha2);
            //ltGne_EstadisticaBE = JsonConvert.DeserializeObject<List<ProxyLocal.Gne_EstadisticaBE>>(jsons);
            
            dataGridView1.DataSource= _TB_IncidentesSOAP.EstadisticaTB_IncidentesRegistroByDepartamento(fecha1,fecha2);
            dataGridView1.Refresh();
            DataTable dt = new DataTable();
            dt.Columns.Add("Departamento_desc", typeof(System.Double));

            dt = GetDataTableFromDGV(dataGridView1);
            dv = dt.DefaultView;

            chrGrafico.Series[0].Points.DataBindXY(dv, "Departamento_Desc", dv, "Incidentes");
            
        }
        private DataTable GetDataTableFromDGV(DataGridView dgv)
        {
            //Recorrer columnas
            DataTable dt1 = new DataTable();
            foreach (DataGridViewColumn columna in dgv.Columns)
            {
                DataColumn col = new DataColumn(columna.Name);
                dt1.Columns.Add(col);
            }
            //Recorrer filas
            foreach (DataGridViewRow fila in dgv.Rows)
            {
                DataRow dr = dt1.NewRow();
                dr[0] = fila.Cells[0].Value.ToString();
                dr[1] = fila.Cells[1].Value.ToString();
                dt1.Rows.Add(dr);
            }
            return dt1;
        }
    }
}
